﻿define(function(require,exports,module) {
    var myPub = require('./public');

    $(function() {

        var oFileAddr = $('.js_file');                      // 图片地址
        var oViewBox = $('.js_view');                       // 成功后预览图
        var oPhotoBox  = $('.js_photoBox');                 // 检测是否上传图片
        var oUpLoadTip = $('.js_upLoadTip');                // 上传身份证图片提示内容
        var oGlobalTxt = $('.js_globalTxt');
        var oRightImg = $('.js_rightImg');                  // 上传成功标识
        var oPreview = $('.js_preview');
        var oIsMsgUpLoad = $('.js_isMsgUpLoad');            // 是不是短信跳过来的
        var _M = 30;                                        // 图兆数
        var maxKB = _M*1024*1024;		                    // 图片最大10M
        var widthSize = 1000;                               // 宽度
        var uploadUrl = '';
        if(oIsMsgUpLoad && oIsMsgUpLoad != undefined && oIsMsgUpLoad.length > 0){
            uploadUrl = base+'/open/saveCardImage';
        }else{
            uploadUrl = base+'/myInfo/saveIDCardImg';
        }

        var layerStyle = 'background-color:rgba(0,0,0,0.6); color:#ffffff; border:none; border-radius: 5px; min-width:50%';

        function AjaxUserImg(obj, src,type,number,name,orderSn,fn) {
            var _Off = false;
            G_JSON_POST(uploadUrl,{
                'base64Voucher':src,
                'category':type,
                'idCardNo':number,
                'name':name,
                'orderSn':orderSn
            },function(data) {
                _Off = true;
                if(data.status == 'success') {
                    if(fn)fn(data);
                }
                else {
                    myPub.TipLayer(data.message, layerStyle);
                }

            },function() {
                // 开始时
            },function () {
                if(!_Off) {
                    myPub.TipLayer("网络异常，请稍后再试！", layerStyle);
                }
            })

        }



        oFileAddr.each(function (i,el) {
            $(el).on('change',function () {
                var _this = this;
                var file = _this.files[0];
                if( !file.type || !/image\/\w+/.test(file.type) ){
                    myPub.TipLayer('您上传的文件格式不对',layerStyle);
                    return
                }
                myPub.TipLayerLong('图片正在识别中......',layerStyle);
                //  限制图片的大小
                if(file.size > maxKB){
                    myPub.TipLayer('图片不能大于'+ _M +'，请重新上传！', layerStyle);
                    return
                }else{
                    // console.log(_this.files[0].size)
                }
                // 图片大小在要求范围内
                lrz(_this.files[0], {width: widthSize}, function (results) {
                    // console.log(results);
                    myPub.TipLayerLong("图片上传中……", layerStyle);
                    var _URL = results.base64;
                    var type;
                    var number = '';
                    var nameFull = '';
                    var bIsReUpLoad = $('.js_previewAreaReUpLoad');
                    var orderSn = '';
                    // $('#img').attr('src',_URL);
                    if(oIsMsgUpLoad && oIsMsgUpLoad != undefined && oIsMsgUpLoad.length > 0){
                        orderSn = orderSnNo;
                    }
                    // 重新上传身份证
                    if(bIsReUpLoad && bIsReUpLoad != undefined && bIsReUpLoad.length>0){
                        number = unescape(sessionStorage.getItem('orderIndCodeOdd'));       // 身份证号码
                        nameFull = unescape(sessionStorage.getItem('orderNameOdd'));        // 收货人名称
                    }else{
                        number = oGlobalTxt.val();              // 身份证号码
                        nameFull = fullName;                    // 收货人名称
                    }

                    if(i == 0){
                        type = 'front';
                    }else if(i == 1){
                        type = 'reverse';
                    }

                    AjaxUserImg(oViewBox, _URL, type, number, nameFull,orderSn,function (data) {
                        var _src = data.message;
                        // 完成时
                        oViewBox.eq(i).find('img').attr('src',  httpImageUrl+_src);
                        oViewBox.eq(i).find('img').css('opacity', 1);
                        oPreview.eq(i).attr('src',  httpImageUrl+_src);
                        // 显示上传成功标识
                        oRightImg.eq(i).show();
                        // 图片显示对应的提示（正反面）去掉
                        oUpLoadTip.eq(i).hide();
                        // 把获取的图片路径储存在对应的input中
                        oPhotoBox.eq(i).val(data.message);
                        myPub.TipLayer("图片保存成功!", layerStyle);
                    });
                });
                $(_this).val('');
            })
        });

        oRightImg.on('click',function () {
            var oImg = $(this).siblings().find('.js_view img');
            var oUpLoad = $(this).siblings().find('.js_upLoadTip');
            $(this).hide();
            oImg.attr('src','');
            oUpLoad.show();
            var index = $(this).attr('data-id')*1;
            oPhotoBox.eq(index).val('');
            oPreview.eq(index).attr('src','');
        })
    });

});



